package cn.edu.ujn.finallab.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.ujn.finallab.dao.Customer;
import cn.edu.ujn.finallab.dao.Orders;
import cn.edu.ujn.finallab.dao.Room;
import cn.edu.ujn.finallab.service.IOrderService;
import cn.edu.ujn.finallab.service.IRoomService;
import cn.edu.ujn.finallab.util.Page;

@Controller
public class OrderController {

	@Autowired
	private IOrderService orderService;

	@Autowired
	private IRoomService roomService;

	// 查询所有的订单
	@RequestMapping("/allOrder")
	public String allOrder(@RequestParam(defaultValue = "1") int currentPage, Model model) {
		Page<Orders> page = orderService.selectByPage(currentPage);
		model.addAttribute("pagemsg", page);// 设置一个属性
		return "orderManager";
	}

	// 办理入住登记
	@RequestMapping("/checkIn")
	public String chenkIn(Model model) {
		List<Orders> list = new ArrayList<Orders>();
		model.addAttribute("orderLost", list);
		model.addAttribute("size", -1);
		return "checkIn";
	}

	// 根据身份证号查询顾客的未入住订单信息
	@RequestMapping("/selectOrderByIdCard")
	public String selectOrderByIdCard(String idCard, Model model) {
		List<Orders> orderList = this.orderService.selectByIdcard(idCard);
		model.addAttribute("orderList", orderList);
		model.addAttribute("size", orderList.size());
		System.out.println(orderList);
		return "checkIn";
	}

	// 为顾客办理入住，根据选择的订单号，修改订单的状态，然后查询客房状态
	// 如果客房不是已入住，将客房状态修改为已入住
	@RequestMapping("/toCheckIn")
	public String toCheckIn(String orderNumber, Model model) {
		// 查询并修改订单
		Orders order = this.orderService.selectByOrderNumber(orderNumber);
		// 修改订单状态
		order.setState("1");
		int updateOrder = this.orderService.updateOrder(order);
		// 查询客房
		Room room = this.roomService.selectByNumber(order.getRoomNumber());
		room.setState("已入住");
		// 修改客房状态
		int updateRoom = this.roomService.updateRoom(room);
		// 都修改完成，返回到原界面
		List<Orders> orderList = this.orderService.selectByIdcard(order.getCustomerIdcard());
		model.addAttribute("orderList", orderList);
		model.addAttribute("size", orderList.size());
		System.out.println(orderList.size());
		System.out.println(orderList);
		return "checkIn";
	}

	// 生成订单
	@RequestMapping("/generateOrder")
	public String generateOrder(String startTime, String endTime, String number, HttpSession session, Model model) {
		// 算出总共入住天数
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");// 年月日
		Date inDate = null;
		Date outDate = null;
		try {
			inDate = sdf1.parse(startTime);
			outDate = sdf1.parse(endTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Calendar c1 = Calendar.getInstance();
		c1.setTime(inDate);
		Calendar c2 = Calendar.getInstance();
		c2.setTime(outDate);
		int days = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);

		// 生成订单号
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String date = df.format(new Date());
		// 根据房号找到房间
		Room room = this.roomService.selectByNumber(number);
		// 从session里取得user对象
		Customer customer = (Customer) session.getAttribute("user");
		// 生成订单
		Orders order = new Orders();
		order.setOrderNumber(date);
		order.setInTime(startTime);
		order.setOutTime(endTime);
		order.setCustomerName(customer.getName());
		order.setCustomerPhone(customer.getPhone());
		order.setCustomerIdcard(customer.getIdcard());
		order.setRoomNumber(number);
		order.setPrice(room.getPrice() * days);
		order.setState("0");
		model.addAttribute("order", order);
		model.addAttribute("type", room.getType());
		session.setAttribute("order", order);
		session.setAttribute("room", room);
		return "showOrder";
	}

	// 用户点击确定后，将订单插入数据库
	@Transactional
	@RequestMapping("/orderConfirm")
	public String orderConfirm(HttpSession session, Model model) {
		// 将房间状态改为已预定
		Room room = (Room) session.getAttribute("room");
		room.setState("已预定");
		int updateRoom = roomService.updateRoom(room);

		// 从session中获取页面传过来的订单对象，将其插入
		Orders order = (Orders) session.getAttribute("order");

		int insert = this.orderService.insertOrder(order);
		if (insert > 0 && updateRoom > 0)
			model.addAttribute("msg", "订单创建成功");
		else
			model.addAttribute("msg", "订单创建失败，请重新选择");

		session.removeAttribute("room");
		session.removeAttribute("order");
		return "bookRoom";
	}

	// 根据输入的信息进行筛选
	@RequestMapping("/searchOrder")
	public String searchOrder(String number, String name, String intime, String outtime,
			@RequestParam(defaultValue = "1") int currentPage, Model model) {
		Page<Orders> page = new Page<Orders>();
		if ((number == null) && (name == null) && (intime == null) && (outtime == null)) {
			page = this.orderService.selectByPage(currentPage);
		} else {
			page = this.orderService.selectByPageOfMany(currentPage, number, name, intime, outtime);
			System.out.println(page.getRows());
		}
		model.addAttribute("pagemsg", page);
		model.addAttribute("msg", "未查到相关信息");
		return "orderManager";
	}
}
